/*-------------------------------------------------------------------------

***************** Modification History ***********************************
   @doc
   @module report.c - Application Report functions |
   @end

$Log: /SoftPay/SPK2SIM/Source/Main/report.c $
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#include <project.h>
#define VSREPT_C

#include <stdlib.h>
#include <stdio.h>
#include <applidl.h>
#include <aclconio.h>
#include <aclstr.h>
#include <svctxo.h>
#include <dbmgr.h>
#include <formater.h>
#include <string.h>


#define NO_SHIFT        0
#define AUTO_SHIFT      1
#define MANUAL_SHIFT    2


/** /#define DEBUG_VSREPORT*/
#ifdef DEBUG_VSREPORT
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <settle.h>
#include <report.h>
#include <review.h>
#include <bdt.h>
#include <cdt.h>
#include <cst.h>
#include <hdt.h>
#include <tblutil.h>
#include <lct.h>
#include <mht.h>
#include <tct.h>
#include <pct.h>
#include <trt.h>
#include <mdlfuncs.h>
#include <msg.h>
#include <msgfuncs.h>
#include <tran.h>
#include <ui.h>
#include <form.h>
#include <pform.h>
#include <form2.h>
#include <format.h>
#include <date.h>
#include <settlbat.h>
#include <accum.h>
#include <confio.h>
#include <spdebug.h>
#include <codesec.h>

/*-------------------------------------------------------------------------
   Function :    vdSetReportHeaderCondition()
    Description :
    Parameters :
    Returns :
    Notes :
--------------------------------------------------------------------------*/

void vdSetReportHeaderCondition(TRANSACTION_OBJECT* pobTran) {
    if (inGetPrinterType() == NOPRINTER || !fGetEnablePrinter())
        return;

    lnHeaderCondition = formater_set_flags(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (strlen(srRcpt.szHeaderLine6) > 0),
                        0,
                        (strlen(srRcpt.szHeaderLine5) > 0),

                        0,
                        (strlen(srRcpt.szHeaderLine4) > 0),
                        0,
                        (strlen(srRcpt.szHeaderLine3) > 0),

                        0,
                        (strlen(srRcpt.szHeaderLine2) > 0),
                        0,
                        (strlen(srRcpt.szHeaderLine) > 0), -1);
}

/*-------------------------------------------------------------------------
   Function :    vdSetSettleReportCondition()
    Description :
    Parameters :
    Returns :
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   10/16/96 Page_K1     Create
--------------------------------------------------------------------------*/

void vdSetSettleReportCondition(VS_BOOL fBatchRpt,VS_BOOL fSettleSucceeded) {
    lnMiddleCondition = formater_set_flags(0, (fGetDemoMode()), (fBatchRpt == VS_FALSE), (fBatchRpt == VS_TRUE), 0, 0, ((fSettleSucceeded == VS_TRUE)), ((fSettleSucceeded == VS_FALSE)), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1);
}

/*-------------------------------------------------------------------------
   Function :    inPrintReportHeader
    Description :
    Parameters :
    Returns :
    Notes :
--------------------------------------------------------------------------*/

int inPrintReportHeader(TRANSACTION_OBJECT* pobTran,PRINTER_OBJECT* obPrinter) {
    /** /pdebug(("--vdMakeReportHeader--"));*/

    inPrintReceiptHeader(pobTran);        

    if (inPrintLines(obPrinter, REPORT_HEADER_START, REPORT_HEADER_END, NO_BL_LINE, lnHeaderCondition) != VS_SUCCESS)
        return(VS_ERR);                     

    return(VS_SUCCESS);
}


/*-------------------------------------------------------------------------
    Function :    inSetTranStatus()
    Description :
    Returns :     None
    Notes :
-------------------------------------------------------------------------*/
void vdSetTranStatus(TRANSACTION_OBJECT* pobTran) {
    char szApprCode[AUTH_CODE_SIZE + 1];
    char szAst[1 + 1];
    char szFL[2 + 1];
    char szF[1 + 1];
    char szM[1 + 1];
    char szSVC[3 + 1];

    srRcpt.szTranStatus[0] = '\0';
    strcpy(szAst, "*");
    strcpy(szFL, "FL");
    strcpy(szF, "f");
    strcpy(szM, "m");
    strcpy(szSVC, "SVC");

    /*
     * Following cdt host index
     * reference stays...
     */
    inLoadHostData(inGetHostHandle());
    inLoadTRTRec(pobTran, inGGCode());

    strcpy(szApprCode, pobTran->srBRec.szAuthCode);

    if (pobTran->srBRec.inNumberOfAdjusts > 0 || pobTran->srBRec.inNumberOfTipAdjusts > 0)
        strcat(srRcpt.szTranStatus, szAst);

    if (fGetCheckFloorLimit(pobTran) && (strncmp(szApprCode, szFL, 2) == 0) || (strncmp(szApprCode, szSVC, 3) == 0))
        strcat(srRcpt.szTranStatus, szF);

    if (pobTran->srBRec.fManual)
        strcat(srRcpt.szTranStatus, szM);
}

/*-------------------------------------------------------------------------
    Function :    inSetReportDateAndTime()
    Description :
    Returns :     None
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   10/11/96  Paula_J1  Creation.
------------------------------------------------------------------------*/

void vdSetReportDateAndTime() {
    char szDate[DATE_SIZE + 1];

    vdGetDate(srRcpt.szDate, YYMMDD);

    if (strlen(srRcpt.szDate) != DATE_SIZE) {
        vdGetDate(szDate, YYMMDD);
        strlcpy(&(szDate[2]), srRcpt.szDate, DATE_SIZE - 2);
        strlcpy(srRcpt.szDate, szDate, DATE_SIZE);
    }
    vdFormatDate(srRcpt.szDate);
    vdGetTime(srRcpt.szTime);
    vdFormatTime(srRcpt.szTime);
}

/*-------------------------------------------------------------------------
    Function :     inMakeTerminalTotals
    Description :  Get batch totals from files and store them in the
                                      receipt structure
    Parameters :   fback (argument for getaccummfname () adds .BAK to file name
    Returns :             VS_SUCCESS no error : VS_ERR otherwise
    Globals :          srRcpt
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   06/19/95  Henry_G1   Added obPrinter parameter.
      2   06/20/95  Henry_G1   Added szFormName parameter.
      3   08/07/95  Ana_C1     This is a generic routine to get the totals,
                               whenever needed.
      4   03/13/96  Helen_D1   Added tip calculation (count and total)
      5   04/10/96  Page_K1    Add storage of sett total records
      6   05/20/96  Page_K1    Handle variable BATCH_NUM_SIZE
                               Add fSettleSucceeded
      7   05/29/96  Page_K1    Decrement BatchNum in SettRec to match
                               BatchNum in detail records
      8   08/29/96  Monique_B1 Replace BATCH_NUM_SIZE with HDT BatchNumSize
      9   10/14/96  Pat_G1     Store host name if settle fails so correct
                               host name is printed on the settlement report.
     10   12/09/97  Page_K1    Remove use of ALL_TOTALS
     11   01/12/98  Page_K1    Subtract credit_trans from debit_trans for
                                grand total.
--------------------------------------------------------------------------*/

int inMakeTerminalTotals(TRANSACTION_OBJECT* pobTran,VS_BOOL fBack,VS_BOOL fSettleSucceeded) {
    ACCUM_REC srHostTotals ;
    int inGrandCount;
    double dbGrandTotal;
    double dbCreditTotal,
    dbDebitTotal;
    long lnBatchNum;


    if (inGetHostTotals(pobTran, &srHostTotals) != VS_SUCCESS)
        return (VS_ERR);

    sprintf(srRcpt.unFrm.srSubTotal.srTerm.szCreditCount, SUBSTITUTE_D, (int) srHostTotals.srTotals[CREDIT_CARD].lnCount[CREDIT_TRAN] + (int) srHostTotals.srTotals[DEBIT_CARD].lnCount[CREDIT_TRAN]);

    /***
    sprintf(srRcpt.unFrm.srSubTotal.srTerm.szCreditTotal, SUBSTITUTE_LD,
            srHostTotals.srTotals[CREDIT_CARD].lnAmount [CREDIT_TRAN] +
            srHostTotals.srTotals[DEBIT_CARD].lnAmount [CREDIT_TRAN]);
    ***/
    dbCreditTotal = srHostTotals.srTotals[CREDIT_CARD].dbAmount[CREDIT_TRAN] + srHostTotals.srTotals[DEBIT_CARD].dbAmount[CREDIT_TRAN];
    vdDb2Str(dbCreditTotal, srRcpt.unFrm.srSubTotal.srTerm.szCreditTotal);
    vdTrimLeadSpaces(srRcpt.unFrm.srSubTotal.srTerm.szCreditTotal);
    vdFormatAmount(srRcpt.unFrm.srSubTotal.srTerm.szCreditTotal, VS_TRUE);
    strcpy(srRcpt.unFrm.srSubTotal.srTerm.szCSymbol, szGetCurSymbol());

    sprintf(srRcpt.unFrm.srSubTotal.srTerm.szDebitCount, SUBSTITUTE_D, (int) srHostTotals.srTotals[CREDIT_CARD].lnCount[DEBIT_TRAN] + (int) srHostTotals.srTotals[DEBIT_CARD].lnCount[DEBIT_TRAN]);

    /***
    sprintf(srRcpt.unFrm.srSubTotal.srTerm.szDebitTotal, SUBSTITUTE_LD,
            srHostTotals.srTotals[CREDIT_CARD].lnAmount [DEBIT_TRAN] +
            srHostTotals.srTotals[DEBIT_CARD].lnAmount [DEBIT_TRAN]);
    ***/

    dbDebitTotal = srHostTotals.srTotals[CREDIT_CARD].dbAmount[DEBIT_TRAN] + srHostTotals.srTotals[DEBIT_CARD].dbAmount[DEBIT_TRAN];
    vdDb2Str(dbDebitTotal, srRcpt.unFrm.srSubTotal.srTerm.szDebitTotal);
    vdTrimLeadSpaces(srRcpt.unFrm.srSubTotal.srTerm.szDebitTotal);

    vdFormatAmount(srRcpt.unFrm.srSubTotal.srTerm.szDebitTotal, VS_TRUE);
    strcpy(srRcpt.unFrm.srSubTotal.srTerm.szCSymbol, szGetCurSymbol());

    /*
     * Set to 0 because number and amount of voids are not stored in this application.
     */
    srRcpt.unFrm.srSubTotal.srTerm.szAdjustCount[0] = '0';
    srRcpt.unFrm.srSubTotal.srTerm.szAdjustCount[1] = 0x00;

    sprintf(srRcpt.unFrm.srSubTotal.srTerm.szAdjustTotal, SUBSTITUTE_D, 0);
    vdFormatAmount(srRcpt.unFrm.srSubTotal.srTerm.szAdjustTotal, VS_TRUE);
    strcpy(srRcpt.unFrm.srSubTotal.srTerm.szCSymbol, szGetCurSymbol());

    /*  ADDED TIP COUNT AND TOTAL FOR DETAIL REPORT....  */
    sprintf(srRcpt.unFrm.srSubTotal.srTerm.szTipCount, SUBSTITUTE_D, srRcpt.unFrm.srDet.srMid.inTotalTipCount);
    sprintf(srRcpt.unFrm.srSubTotal.srTerm.szTipTotal, SUBSTITUTE_LD, srRcpt.unFrm.srDet.srMid.lnTotalTipAmt);
    vdFormatAmount(srRcpt.unFrm.srSubTotal.srTerm.szTipTotal, VS_TRUE);
    strcpy(srRcpt.unFrm.srSubTotal.srTerm.szCSymbol, szGetCurSymbol());


    inGrandCount = (int) srHostTotals.srTotals[CREDIT_CARD].lnCount[CREDIT_TRAN] + (int) srHostTotals.srTotals[DEBIT_CARD].lnCount[CREDIT_TRAN] + (int) srHostTotals.srTotals[CREDIT_CARD].lnCount[DEBIT_TRAN] + (int) srHostTotals.srTotals[DEBIT_CARD].lnCount[DEBIT_TRAN];
    dbGrandTotal = srHostTotals.srTotals[CREDIT_CARD].dbAmount[DEBIT_TRAN] + srHostTotals.srTotals[DEBIT_CARD].dbAmount[DEBIT_TRAN] - srHostTotals.srTotals[CREDIT_CARD].dbAmount[CREDIT_TRAN] - srHostTotals.srTotals[DEBIT_CARD].dbAmount[CREDIT_TRAN];


    sprintf(srRcpt.unFrm.srSubTotal.srTerm.szNetCount, SUBSTITUTE_D, inGrandCount);

    /*    sprintf(srRcpt.unFrm.srSubTotal.srTerm.szNetTotal, SUBSTITUTE_LD, lnGrandTotal); */
    vdDb2Str(dbGrandTotal, srRcpt.unFrm.srSubTotal.srTerm.szNetTotal);
    vdTrimLeadSpaces(srRcpt.unFrm.srSubTotal.srTerm.szNetTotal);

    vdFormatAmount(srRcpt.unFrm.srSubTotal.srTerm.szNetTotal, VS_TRUE);

    /* Put settlement totals in host settlement file */
    if (fSettleSucceeded) {
        /* Batch Num has already been bumped up in live but not demo */
        lnBatchNum = (fGetDemoMode() != VS_TRUE ? lnGetBatchNum() - 1L : lnGetBatchNum());
        if (lnBatchNum == 0)  /* the batch number had reached the max */ {
            lnBatchNum = 9L;
            long2str(pobTran->szBatchNum, lnBatchNum);

            /* Since the batch number was the maximum value possible
             * recreate the max batch number for the Host
             * using the max batch number size from the HDT
             */
            pad(pobTran->szBatchNum, pobTran->szBatchNum, '9', inGetBatchNumSize(), RIGHT);
        } else {
            long2str(pobTran->szBatchNum, lnBatchNum);
            pad(pobTran->szBatchNum, pobTran->szBatchNum, '0', inGetBatchNumSize(), RIGHT);
        }
    } else /* pj1 */ {
        strcpy(srRcpt.szHostName, szGetHostName());
        long2str(pobTran->szBatchNum, lnGetBatchNum());
        pad(pobTran->szBatchNum, pobTran->szBatchNum, '0', inGetBatchNumSize(), RIGHT);
    }

    strlcpy(srRcpt.szBatchNum, pobTran->szBatchNum, BATCH_NUM_SIZE);

    return(VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    Function :    vdLoadDateTime
    Description : Loads the date and time variable from the GDS to the receipt
                  structure and formats them.
    Parameters :
    Returns :
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   09/17/96 Jim_B4     Create
----------------------------------------------------------------------------*/

void vdLoadDateTime(TRANSACTION_OBJECT* pobTran) {
    strlcpy(srRcpt.szTime, pobTran->szSettTime, DATE_SIZE);
    vdFormatTime(srRcpt.szTime);
    strlcpy(srRcpt.szDate, pobTran->szSettDate, DATE_SIZE);
    vdFormatDate(srRcpt.szDate);
}

/*-------------------------------------------------------------------------
    Function :    inMakeReportHeader()
    Description :
    Parameters : Receipt Type(original, duplicate)
    Returns :
    Notes :
-------------------------------------------------------------------------*/

void vdMakeReportHeader(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--vdMakeReportHeader--"));*/
    vdMakeFooter(pobTran);
    vdSetReportHeaderCondition(pobTran);
    vdSetReportDateAndTime();
    vdClearFooter();
    strcpy(srRcpt.szHostName, szGetHostName());
}

int inCheckMultiCurrency(TRANSACTION_OBJECT* pobTran) {
    int inHost;
    int inTotalHosts;
    unsigned char ucTempHDTRec[SIZE_HDT_REC + 1];
    //  HDT_REC srTempHDTRec;

    inTotalHosts = MAX_HOSTS;
    for (inHost = 0; inHost < MAX_HOSTS; inHost++) {
        if (inLoadHDTRecFromStruct(inHost, ucTempHDTRec) != VS_SUCCESS) {
            vdSGErrorMessage(HDT_ERR_MSG21);
            return(VS_ERR);
        }
        if (fGetAllowMultiCurrencyFromStruct(ucTempHDTRec)) {
            vdDisplayErrorMessage(REPORT_NOT_ALLOWED_MSG21);
            return (VS_ESCAPE);
        }
    }

    return(VS_SUCCESS);
}



int REPORT_CODE_CHECK(char* a,char* b,short* c,char* d,unsigned char* e) {
    GET_VER(a, b, c, d, e);
    return 1;
}



#undef VSREPT_C

